package com.example.admin.mqtt.mqttconfig;

import com.example.admin.mqtt.IMqttClient;
import com.example.admin.mqtt.MqttClient;
import com.example.admin.mqtt.MqttClientOptions;


import com.example.admin.service.Lotservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



import static com.example.admin.mqtt.MqttConstants.*;


@Configuration
public class MqttClientconfig {
    @Autowired
    Lotservice lotservice;
    @Bean
    public IMqttClient createclient(){
        IMqttClient mqttClient = new MqttClient(MqttClientOptions.builder()
                .host(HOST)
                .port(PORT)
                .accessKey(ACCESS_KEY)
                .accessCode(ACCESS_CODE)
                .instanceId(INSTANCE_ID)
                .build());
        mqttClient.setRawMessageListener(message -> {
            lotservice.printmessage(message);
        });
        mqttClient.connect();
        mqttClient.subscribeTopic(SUBSCRIBE_TOPIC, null);
        return mqttClient;
    }


}
